package com.ecode.ecny.base;

import com.ecode.ecny.exception.TrxErrorEnums;
import com.ecode.ecny.exception.TrxException;
import com.ecode.ecny.uitl.DateTimeUtil;
import lombok.Data;

/**
 * @ClassName Head
 * @Author mjlft
 * @Date 2021/8/12 21:15
 * @Version 1.0
 * @Description 响应头
 */
@Data
public class RspHead {

    //错误码
    private String retCode;

    //错误描述
    private String retMsg;

    //交易状态
    private String trnStatus;

    //响应日期
    private String retDate;

    //响应时间
    private String retTime;

    /**
     * 生成响应头
     * @param ex
     * @return
     */
    public static RspHead createHead(Exception ex) {
        if(ex == null){
            return createHead(TxnStatus.SUCC, AppContext.RET_SUCC_CODE, AppContext.RET_SUCC_MSG);
        }
        if (ex instanceof TrxException) {
            return createHead(((TrxException) ex).getTrxStatus(), ((TrxException) ex).getTrxCode(), ((TrxException) ex).getTrxMsg());
        } else {
            return createHead(TxnStatus.FAIL, TrxErrorEnums.SYS_ERROR.getCode(), TrxErrorEnums.SYS_ERROR.getMsg());
        }
    }

    /**
     * 生成响应头
     *
     * @param trnStatus
     * @param retCode
     * @param retMsg
     * @return
     */
    public static RspHead createHead(String trnStatus, String retCode, String retMsg) {
        RspHead rspHead = new RspHead();
        rspHead.setTrnStatus(trnStatus);
        rspHead.setRetCode(retCode);
        rspHead.setRetMsg(retMsg);
        rspHead.setRetDate(DateTimeUtil.getCurrentDate());
        rspHead.setRetTime(DateTimeUtil.getCurrentTime());

        return rspHead;
    }
}
